package com.canbot.u05.activity.agenda.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import com.canbot.u05.R
import com.canbot.u05.activity.agenda.AgendaHomeActivity
import com.canbot.u05.activity.agenda.calendar.formatDateStr
import com.canbot.u05.activity.agenda.calendar.getDateFromTable
import com.canbot.u05.activity.agenda.interfaces.IDateSelect
import com.canbot.u05.activity.presidemode.newcode.view.NHostBaseDialog
import com.haibin.calendarview.Calendar
import com.haibin.calendarview.CalendarView
import com.haibin.calendarview.CalendarView.OnMonthChangeListener
import kotlinx.android.synthetic.main.agenda_date_dialog.*
import java.util.*

/**
 * @author yinxiaowei
 * @date 2019/5/31
 */
class DateDialog(context: Context, multiSelect: Boolean, showScheme: Boolean, var iDateSelect: IDateSelect) : NHostBaseDialog(context), View.OnClickListener, CalendarView.OnCalendarMultiSelectListener,
        OnMonthChangeListener {

    private val TAG = DateDialog::class.java.simpleName
    override fun onMonthChange(year: Int, month: Int) {
        dateTitle.text = formatDateStr(year, month)
    }

    override fun onCalendarMultiSelectOutOfRange(calendar: Calendar) {
    }

    override fun onMultiSelectOutOfSize(calendar: Calendar, maxSize: Int) {

    }

    private fun mockData() {
        val map = HashMap<String, Calendar>()
        map["1"] = getSchemeCalendar(1)
        map["2"] = getSchemeCalendar(2)
        val year = calendarView.curYear
        val month = calendarView.curMonth
        map[getSchemeCalendar(year, month + 1, 22).toString()] = getSchemeCalendar(year, month + 1, 22)
        map[getSchemeCalendar(year, month, 29).toString()] = getSchemeCalendar(year, month, 29)
        calendarView.setSchemeDate(map)
    }

    private fun getShcemaData() {
        val size = AgendaHomeActivity.agendaTableList.size
        if (size > 0) {
            val map = HashMap<String, Calendar>()
            AgendaHomeActivity.agendaTableList.forEach {
                if (it.agendaDataTableList.size > 0) {
                    if (it.week != -1) { //星期标记
                        map[it.week.toString()] = getSchemeCalendar(it.week)
                    } else { // 日期标记
                        map[getSchemeCalendar(getDateFromTable(it.date)).toString()] = getSchemeCalendar(getDateFromTable(it.date))
                    }
                }
            }
            calendarView.setSchemeDate(map)
        }
    }

    private fun getSchemeCalendar(intArray: IntArray): Calendar {
        val calendar = Calendar()
        calendar.year = intArray[0]
        calendar.month = intArray[1]
        calendar.day = intArray[2]
        calendar.addScheme(Calendar.Scheme())
        return calendar
    }

    private fun getSchemeCalendar(year: Int, month: Int, day: Int): Calendar {
        val calendar = Calendar()
        calendar.year = year
        calendar.month = month
        calendar.day = day
        calendar.addScheme(Calendar.Scheme())
        return calendar
    }

    private fun getSchemeCalendar(week: Int): Calendar {
        val calendar = Calendar()
        calendar.week = week
        calendar.addScheme(Calendar.Scheme())
        return calendar
    }

    @SuppressLint("SetTextI18n")
    override fun onCalendarMultiSelect(calendar: Calendar, curSize: Int, maxSize: Int, isSelect: Boolean) {
        Log.e(TAG, "onCalendarMultiSelect  -- " + calendar.year +
                "  --  " + calendar.month +
                "  -- " + calendar.day + "   " + isSelect)
        dateTitle.text = formatDateStr(calendar.year, calendar.month)
        val date: java.util.Calendar = java.util.Calendar.getInstance()
        date.timeInMillis = calendar.timeInMillis
        iDateSelect.onSelect(isSelect, date)
    }

    init {
        val view = LayoutInflater.from(this.context).inflate(R.layout.agenda_date_dialog, null)
        setContentView(view)
        calendarView.setMultiSelect(multiSelect)
        calendarView.setOnCalendarMultiSelectListener(this)
        calendarView.setOnMonthChangeListener(this)
        dateTitle.text = formatDateStr(calendarView.curYear, calendarView.curMonth)
        btn_date_cancle.setOnClickListener(this)
        btn_date_confirm.setOnClickListener(this)
        date_prev.setOnClickListener(this)
        date_next.setOnClickListener(this)
//        mockData()
        if (showScheme) {
            getShcemaData()
        } else {
            calendarView.setRange(calendarView.curYear, calendarView.curMonth, calendarView.curDay, 2055, 12, 22)
        }
    }

    override fun onClick(v: View) {
        when (v) {
            btn_date_cancle -> {
                dismiss()
            }
            btn_date_confirm -> {
                dismiss()
                iDialogBtnLisenter?.onConfirm(null)
            }
            date_prev -> {
                calendarView.scrollToPre()
            }
            date_next -> {
                calendarView.scrollToNext()
            }
        }
    }

    override fun setDialogWindowAttr() {
        setCanceledOnTouchOutside(true)
        val window = this.window
        val lp = window.attributes
        lp.gravity = Gravity.CENTER
        var screenW = window.windowManager.defaultDisplay.width
        lp.width = screenW - this.context.resources.getDimension(R.dimen.px900).toInt()//宽高可设置具体大小
        lp.height = this.context.resources.getDimension(R.dimen.px740).toInt()
        this.window.attributes = lp
    }
}
